﻿<%@ Page Title="端头陈列量设置" Language="C#" MasterPageFile="~/master/m.master" %>

<script runat="server"></script>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
    <link href="../assets/css/plugins/footable/footable.core.css" rel="stylesheet">
    <link href="../assets/css/shelf/base.css" rel="stylesheet">
    <link href="../assets/css/plugins/webuploader/webuploader.css" rel="stylesheet" />
    <link href="../assets/img/validate/validate.css" rel="stylesheet">
    <link href="../assets/css/plugins/iCheck/customsmall.css" rel="stylesheet" />
    <link href="../assets/css/plugins/stepper/stepper.widget.css" rel="stylesheet" />
    <link href="../assets/css/plugins/boxdrop/boxDrop.css" rel="stylesheet" />

    <style type="text/css">
        .system-box .right-side {
            width: 98%;
        }

        .condition, .condition-title {
            border-bottom: 1px solid #dfdfdd;
        }

        .form-group label {
            max-width: 70px;
            margin-left: 5px;
            padding-left: 1px;
            padding-right: 1px;
        }

        @media (min-width: 750px) {
            .modal-dialog {
                width: 750px;
            }

            .footable {
                border-bottom: 1px solid #dfdfdd;
                border-left: 1px solid #dfdfdd;
            }

                .footable th,
                .footable td {
                    border-top: 1px solid #dfdfdd;
                    border-right: 1px solid #dfdfdd;
                    text-align: center;
                }

            .tb-head-container {
                padding-right: 6px;
                background: #fff;
            }

            .tb-body-container {
                overflow-x: hidden;
                padding-right: 0px;
                background: #fff;
            }
            /*所有表格行高缩小*/
          .ibox-content  .table > tbody > tr > td, .table > tbody > tr > th, .table > tfoot > tr > td, .table > tfoot > tr > th, .table > thead > tr > td, .table > thead > tr > th {
                padding: 1px !important;
            }
           
        }
        #addWindow .form-group label {
        max-width:85px;
        }
    </style>

</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="main" runat="Server">
    <div class="system-box">
        <div class="right-side">
            <%-- 按钮--%>
            <div class="btns-container"></div>
            <%--新增弹出框begin--%>
            <div class="modal inmodal" id="addWindow" tabindex="-1" role="dialog" aria-hidden="true">
                <div class="modal-dialog">
                    <div class="modal-content animated bounceInRight">
                        <div class="modal-header">
                            <span class="editTitle"></span>
                            <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span> <span class="sr-only">关闭</span> </button>
                        </div>
                        <div id="message" style="display: none"></div>
                        <div class="modal-body clearfix">
                            <form class="form-horizontal">
                                <div class="form-group">
                                    <label for="CompanyID" class="col-sm-3 control-label">公司编码</label>
                                    <div class="col-sm-9">
                                        <input type="text" id="CompanyID" name="CompanyID" placeholder="" class="form-control" readonly="readonly" value="" />
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="shopSearch" class="col-sm-3 control-label">店&nbsp;&nbsp;铺:</label>
                                    <div class="col-sm-9">
                                        <input type="text" id="shopSearch" name="shopSearch" placeholder="请输入店铺代码或名称" datacol="yes" err="店铺" checkexpession="NotNull" class="form-control required" />
                                        <span style="color: red; display: none; font-size: 8px;"  id="ShopTip">无对应门店，或已被使用了！</span>
                                     </div>
                                   
                                </div>

                                <div class="form-group">
                                    <label for="ShopCode" class="col-sm-3 control-label">店铺编号:</label>
                                    <div class="col-sm-9">
                                        <input type="text" id="ShopCode" name="ShopCode" placeholder="" datacol="yes" err="店铺编号" checkexpession="NotNull" class="form-control required" readonly />
                                        <input type="text" id="OldShopCode" name="OldShopCode" placeholder="" datacol="yes" err="店铺编号" class="hide" readonly />
                                    </div>
                                </div>

                                <div class="form-group">
                                    <label for="ShopID" class="col-sm-3 control-label">店铺内码:</label>
                                    <div class="col-sm-9">
                                        <input type="text" id="ShopID" name="ShopID" placeholder="" datacol="yes" err="店铺内码" checkexpession="NotNull" class="form-control required" readonly />
                                        <input type="text" id="OldShopID" name="OldShopID" placeholder="" datacol="yes" err="店铺内码" class="hide" readonly />
                                    </div>
                                </div>

                                <div class="form-group">
                                    <label for="MaterialID" class="col-sm-3 control-label">货号:</label>
                                    <div class="col-sm-9">
                                        <input type="text" id="MaterialID" name="MaterialID" placeholder="" datacol="yes" err="货号" checkexpession="NotNull" maxlength="30" class="form-control required" />
                                        <input type="text" id="OldMaterialID" name="OldMaterialID" placeholder="" datacol="yes" err="货号" class="hide" />
                                    </div>

                                </div>

                                <div class="form-group">
                                    <label for="DisplayQty" class="col-sm-3 control-label">最低陈列量:</label>
                                    <div class="col-sm-9">
                                        <input type="text" id="OldDisplayQty" name="OldDisplayQty" class="hide" />
                                        <input type="text" id="DisplayQty" name="DisplayQty" placeholder="请输入最低陈列量" datacol="yes" checkexpession="NotNull" err="最低陈列量" maxlength="3" class="form-control required" />
                                    </div>
                                </div>

                            </form>
                        </div>
                        <div class="modal-footer">
                            <button type="button" id="addModuleBtn" class="btn btn-primary">保存</button>
                            <button type="button" id="closeAddBox" class="btn btn-white" data-dismiss="modal">关闭</button>
                        </div>
                    </div>
                </div>
            </div>
            <%--新增弹出框end--%>


            <div class="condition clearfix">
                <label>店铺&nbsp;&nbsp;</label>
                <input class="" id="search-Shop" placeholder="输入店铺名称或编号" style="display: none;" />
                <input type="text" placeholder="店铺代码或名称" id="search-store" data-value="" class="form-control input-w170" />
                <label>货号</label>
                <input id="search_materialID" class="form-control input-w170" placeholder="货号" />
            </div>

            <div class="right-content-box clearfix">
                <div class="ibox-content">
                    <table class="footable table table-stripped">
                        <thead></thead>
                        <tbody id="pageList"></tbody>
                    </table>
                </div>
            </div>

            <div class="pageControl"></div>
        </div>
    </div>

</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="footer" runat="Server">
    <script src="../assets/js/jquery-ui-1.10.4.min.js"></script>
    <script src="../assets/img/validate/learunui-validator.js"></script>
    <script src="../assets/js/page/pageControl.js"></script>
    <script src="../assets/js/page/rScroll.js"></script>
    <script src="../assets/js/page/tableControl.js"></script>
    <script src="../assets/js/plugins/webuploader/webuploader.min.js"></script>
    <script src="../assets/js/framework.js"></script>
    <script src="../assets/js/plugins/bootstrap-typeahead/bootstrap-typeahead.js"></script>

    <script src="../assets/js/plugins/iCheck/icheck.min.js"></script>
    <script src="../assets/js/plugins/iCheck/icheckAll.js"></script>
    <script src="../assets/js/plugins/stepper/stepper.widget.js"></script>
    <script src="../assets/js/plugins/boxdrop/jquery.boxdrop.js"></script>
    <script>
        $('document').ready(function () {
            //模块ID
            var ModuleID = Y.getUrlParam("ModuleID");
            //当前页数
            var page = 1;
            commonUtil.loadBtnList(ModuleID, function () {
                $('#editBtn').attr("disabled", "disabled");
                //$('#deleteBtn').attr("disabled", "disabled");
                rScroll();
            });

            var dataList = [];
            IsCreate = true;
            //处理表头
            if ($('.footable thead').children().size() == 0) {
                tableControl.initThead('.footable',
                    [
                        {
                            trClass: '',
                            children: [
                                { tdText: '', c: 0, r: 0, tdHtml: '<input type="checkbox" id="checkAllPlan" />', tdId: '', tdClass: 'diy-td-w30' },
                                { tdText: '店铺编号', c: 0, r: 0, tdHtml: '', tdId: '', tdClass: 'diy-td-w50' },
                                { tdText: '店铺名称', c: 0, r: 0, tdHtml: '', tdId: '', tdClass: 'diy-td-w150' },
                                { tdText: '货号', c: 0, r: 0, tdHtml: '', tdId: '', tdClass: 'diy-td-w100' },
                                { tdText: '最低陈列量', c: 0, r: 0, tdHtml: '', tdId: '', tdClass: 'diy-td-w50' }
                            ]
                        }
                    ]);
            }

            //功能无法使用，用js判断
            $('#DisplayQty').bind('input propertychange', function () {
                if (isNaN($(this).val())) {
                    $(this).val($(this).val().substring(0, $(this).val().length-1));
                    return;
                }
                if ($(this).val().length > 3) {
                    return;
                    //$(this).val($(this).val().substring(0,3));
                } 
            }); 
            $('#MaterialID').bind('input propertychange', function () {
                var myReg = /^[\u4e00-\u9fa5]+$/; 
                if (myReg.test($(this).val())) {
                    $(this).val("");
                }
                //将中文替换掉
                $(this).val($(this).val().replace(/[^\x00-\xff]/g, ''));
            });
           

            var pageList = [];
            //获取列表
            var loadList = function () {
                $('#editBtn').attr('disabled', 'disabled');
                shopCode = '';
                materialID = '';
                sizeID = '';
                pageControl.init('EndsDisplayMeasure', 'GetEndsDisplayMeasurelist', {
                    keys: $('#search-store').val().trim().replace(/'/g, ''),
                    materialId: $('#search_materialID').val(),
                    ShopList: $('#search-Shop').val().trim(),
                    Page: page,
                    Size: 50,
                    needLayer: true,
                    alwaysCallBack: function () {
                        $('#pageList').empty();
                    }
                }, function (result) {
                    pageList = [];
                    page = result.page;
                    var tBody = [];
                    if (!Y.checkNull(result.data)) {
                        if (result.data.Items.length > 0) {
                            //有值，可以编辑  在选择后控制
                            //$('#editBtn').removeAttr('disabled');
                            dataList = result.data.Items;
                            var IsEmpty = '0';
                            result.data.Items.map(function (items, i) {
                                //pageList.push(item);
                                items.map(function (item, j) {
                                    var tempChild = [];
                                    if (j == 0) {
                                        tempChild = [
                                            {
                                                tdText: '',
                                                c: 0,
                                                r: items.length,
                                                tdHtml:
                                                '<input type="checkbox" class="checkPlan" data-index="' + i + '" />',
                                                tdId: '',
                                                tdClass: ''
                                            },
                                            {
                                                tdText: '',
                                                c: 0,
                                                r: items.length,
                                                tdHtml: '<span style="display:inline-block; width:100px;">' +
                                                (item.CompanyID || IsEmpty) + '</span>',
                                                tdId: '',
                                                tdClass: 'hide CompanyID'
                                            },
                                            {
                                                tdText: '',
                                                c: 0,
                                                r: items.length,
                                                tdHtml: '<span style="display:inline-block; width:100px;">' +
                                                (item.ShopID || IsEmpty) + '</span>',
                                                tdId: '',
                                                tdClass: 'hide ShopID'
                                            },
                                            {
                                                tdText: '',
                                                c: 0,
                                                r: items.length,
                                                //tdHtml: ((item.Flag == 1) ? '<i class="fa fa-lightbulb-o"></i>' : '') + '<span style="display:inline-block; width:50px">' +
                                                tdHtml: '<span style="display:inline-block; width:50px">' +
                                                (item.ShopCode || IsEmpty) + '</span>',
                                                tdId: '',
                                                tdClass: 'ShopCode'
                                            },
                                            {
                                                tdText: '',
                                                c: 0,
                                                r: items.length,
                                                tdHtml: '<span style="display:inline-block; width:150px;">' +
                                                (item.ShopName || IsEmpty) + '</span>',
                                                tdId: '',
                                                tdClass: 'ShopName'
                                            },
                                            {
                                                tdText: '',
                                                c: 0,
                                                r: items.length,
                                                tdHtml: '<span style="display:inline-block; width:100px;">' +
                                                (item.MaterialID || IsEmpty) + '</span>',
                                                tdId: '',
                                                tdClass: 'MaterialID'
                                            },
                                            {
                                                tdText: '',
                                                c: 0,
                                                r: items.length,
                                                tdHtml: '<span style="display:inline-block; width:100px;">' +
                                                (item.DisplayQty || IsEmpty) + '</span>',
                                                tdId: '',
                                                tdClass: 'DisplayQty'
                                            }

                                        ];
                                    }
                                    var tempTr = {
                                        trClass: (i % 2 == 0 ? 'gradeX footable-even' : 'gradeX footable-odd') + ' '
                                        + item.ShopID + '_'
                                        + item.MaterialID + '_'
                                        + item.DisplayQty + '_',
                                        children: tempChild
                                    };
                                    tBody.push(tempTr);
                                });
                            });
                        }
                        rScroll();
                    }
                    tableControl.initTbody('.footable', (tBody.length > 0 ? tBody : null), 13, null);
                    //选择全部插件
                    $(".footable").icheckAll({
                        checkObj: ".checkPlan",//表格下面的选择控件对象 选择器
                        checkAllObj: "#checkAllPlan",//全选 选择器
                        clickoutBoxCheck: false,//点击行 不选中
                        checkboxMargin: "4px 0 0 0 "//上右下左
                    });
                });
            }
            //页面加载
            loadList();
            //店铺选择
            $('#search-Shop').boxDrop({ width: 120 });
            $('#search-Shop').on("boxdrop_open", function () {
                $('#search-Shop').boxDrop("load", { ParentAreaID: $("#search-ParentAreaID").val() });
            });
            //查询按钮
            $('.btns-container').on('click', '#btn-search', function () {
                page = 1;
                loadList();
            });
            //选中记录样式变化
            $('#pageList').on('click', 'tr', function () {
                shopCode = $(this).find('.Flag span').attr("data-ShopCode");
                materialID = $(this).find('.Flag span').attr("data-MaterialID");
                DisplayQty = $(this).find('.Flag span').attr("data-DisplayQty");
                // flag = $(this).find('.Flag').text();
                //if (!Y.checkNull(shopCode) && !Y.checkNull(materialID)) {
                $('#editBtn').removeAttr('disabled');
                //}

                $('.footable tr').removeClass('check');

                var trClass = $(this).attr("class");
                trClass = trClass.replace(/gradeX /, "");
                trClass = trClass.replace(/footable-even /, "");
                trClass = trClass.replace(/footable-odd /, "");
                trClass = '.' + trClass;
                $(trClass).addClass("check");

            });


            //删除按钮
            commonUtil.initDeleteBtnEvent({
                callBack: function () {
                    submitList = [];
                    $(".footable").find('.checked').each(function () {
                        var ShopCode = $(this).parents("td").siblings(".ShopCode").text();
                        var MaterialID = $(this).parents("td").siblings(".MaterialID").text();
                        submitList.push({
                            ShopCode: ShopCode,
                            MaterialID: MaterialID,
                        });
                    });
                    if (submitList.length == 0) {
                        swal('请选择要删除的项');
                        return false;
                    }
                    // console.table(submitList);
                    Y.API2('EndsDisplayMeasure', 'Remove',
                        {
                            List: submitList
                        }
                        , function (data) {
                            layer.closeAll('dialog');
                            if (data.Success == false) {
                                swal('删除失败', data.Message, 'error');
                            } else {
                                swal('删除成功', '该信息已被你永久删除', 'success');
                                loadList();
                            }
                        });
                }
            });

            //新增按钮
            $('.btns-container').on('click', '#addBtn', function () {
                $('.editTitle').text('新增端头陈列量');
                $('#addModuleBtn').show();
                IsCreate = true;
                $("#shopSearch").removeAttr('readonly');
                $("#DisplayQty").removeAttr('readonly');
                resetInput();
            });
            var resetInput = function () {
                $("#shopSearch").val("");
                $("#ShopCode").val("");
                $("#ShopID").val("");
                $("#MaterialID").val("");
                $("#DisplayQty").val("");
                var data = sessionStorage.getItem('login-info');
                var d = $.parseJSON(data);
                if (!Y.checkNull(d.CompanyID))
                    $('#CompanyID').val(d.CompanyID);

            };

            //编辑按钮
            $('.btns-container').on('click', '#editBtn', function () {
                $('.editTitle').text('新增端头成列量');
                $('#addModuleBtn').show();
                resetInput();
                IsCreate = false;
                loadDetail();
            });
            //获取选中行的内容,填充到打开的框内
            var loadDetail = function () {
                //有的会加载出来两个，暂时用这个代替,bug滞后处理
                var $check = $('.footable').find('.check')[0];
                var CompanyID = $($check).find('.CompanyID').find("span").html();
                var ShopID = $($check).find('.ShopID').find("span").html();
                var ShopCode = $($check).find('.ShopCode').find("span").html();
                var ShopName = $($check).find('.ShopName').find("span").html();
                var MaterialID = $($check).find('.MaterialID').find("span").html();
                var DisplayQty = $($check).find('.DisplayQty').find("span").html();

                if (CompanyID == "") {
                    swal("数据异常，公司编码不能为空，请刷新页面重试", '', 'error');
                    return false;
                }
                if (shopCode == "") {
                    swal("数据异常，店铺编码不能为空，请刷新页面重试", '', 'error');
                    return false;
                }
                if (materialID == "") {
                    swal("数据异常，货号不能为空，请刷新页面重试", '', 'error');
                    return false;
                }
                if (ShopID == "") {
                    swal("数据异常，店铺内码不能为空，请刷新页面重试", '', 'error');
                    return false;
                }
                $('#shopSearch').val(ShopName);
                $('#ShopCode').val(ShopCode);
                $('#ShopID').val(ShopID);
                $('#CompanyID').val(CompanyID);
                $('#MaterialID').val(MaterialID);
                $('#DisplayQty').val(DisplayQty);
                //保存旧的
                $('#OldShopCode').val(ShopCode);
                $('#OldShopID').val(ShopID);
                $('#OldMaterialID').val(MaterialID);
                $('#OldDisplayQty').val(DisplayQty);

            }


            //自动填充(商店)
            $('#shopSearch').typeahead({
                source: function (query, process) {
                    var parameter = { query: query };
                    //$('#shopSearch').val('');
                    //$('#ShopCode').val('');
                    //$('#ShopID').val('');
                    Y.API2('DwShop','GetShopList',{Keys: $('#shopSearch').val()},
                        function (data) {
                            if (data.Success == false || data.length <= 0) {
                                //无数据，做清空处理
                                $('#shopSearch').val('');
                                $('#ShopCode').val('');
                                $('#ShopID').val('');
                                $("#ShopTip").show()
                                setTimeout("$('#ShopTip').hide()", 1000);
                                swal(data.Message, '', 'error');
                                //console.log(data.Message);
                            } else {
                                var results = data.map(function(item) {
                                    return JSON.stringify(item);
                                });
                                process(results);
                               
                                //无数据做提示
                                var style = $(".typeahead").attr("style");
                                if ((style == "top: 34px; left: 15px; display: none;" || style == "left: 15px; top: 34px; display: none;") && $(".typeahead li").length >= 1) {
                                    //$("#ShopTip").text("无此门店，或已被使用了！");
                                    $("#ShopTip").show()
                                    setTimeout("$('#ShopTip').hide()", 1000);
                                    return;
                                }
                                else if (style == undefined && $(".typeahead li").length == 0) {
                                    //$("#ShopTip").text("无此门店，或已被使用了！");
                                    $("#ShopTip").show();
                                    setTimeout("$('#ShopTip').hide()", 1000);
                                    return;
                                }
                              
                            }
                        });
                },
                highlighter: function(item) {
                    var item = JSON.parse(item); 
                    return '名称：' + item.ShopName + '　编号：' + item.ShopCode;
                },
                updater: function(item) {
                    var item = JSON.parse(item);
                    $('#shopSearch').val(item.ShopName);
                    $('#ShopCode').val(item.ShopCode);
                    $('#ShopID').val(item.ShopID);
                    return item.ShopName;
                }
        });
            //清除残留数据
            $('#shopSearch').blur(function () {
                if ($(this).val() == "" || $(this).val() == undefined)
                {
                     $('#ShopCode').val('');
                    $('#ShopID').val('');
                }
            });


            //弹窗的保存操作
            $('#addModuleBtn').click(function () {
                if (!CheckDataValid('.form-horizontal')) {
                    return false;
                }
                var action = "";
                var successText = "";
                if (IsCreate) {
                    action = "Insert";
                    successText = '新增成功';
                }
                else {
                    action = "Update";
                    successText = '编辑成功';
                    //效验是否未更改内容
                    if (CheckIsChang()) {
                        tipCss($(this), "数据未更改"); return false;
                        return false; 
                    }
                }
                layer.msg('玩命加载中……', { icon: 16, shade: 0.5, time: 0, maxWidth: 200 });
                Y.API2('EndsDisplayMeasure', action, {
                    OldShopCode: $('#OldShopCode').val(),
                    OldMaterialID: $('#OldMaterialID').val(),
                    OldShopID: $('#OldShopID').val(),
                    ShopCode: $('#ShopCode').val(),
                    ShopName: $('#shopSearch').val(),
                    ShopID: $('#ShopID').val(),
                    MaterialID: $('#MaterialID').val(),
                    DisplayQty: $('#DisplayQty').val()
                }, function (data) {
                    layer.closeAll('dialog');
                    if (data.Success == false) {
                        swal(data.Message, '', 'error');
                    } else {
                        swal(successText, '', 'success');
                        //关闭新建  or 编辑框  
                        $('#closeAddBox').click();
                        //刷新列表
                        loadList();
                    }
                });
            });
            //修改时 验证是否有修改内容
            var CheckIsChang = function () {
                
                var OldShopCode = $('#OldShopCode').val();
                var OldMaterialID = $('#OldMaterialID').val();
              //  var OldShopID = $('#OldShopID').val();
                var OldDisplayQty = $('#OldDisplayQty').val();
                var ShopCode = $('#ShopCode').val();
              //  var ShopID = $('#ShopID').val();
                var MaterialID = $('#MaterialID').val();
                var DisplayQty = $('#DisplayQty').val();
                 
                if (OldShopCode == ShopCode && OldMaterialID == MaterialID && OldDisplayQty == DisplayQty)//&& OldShopID == ShopID) //ShopID先不用
                    return true;

                return false;
            }

            //导入
            $(".btns-container").on("click", '#importBtn', function () {
                $.ImportExcelTemplate({
                    filetype: 'ENDSDISPLAYMEASURE',
                    filename: '端头陈列量设置',
                    //conditions: getConditions(),
                    action: 'ImportTemplate',
                    state: 'hasInfo',
                    after: function () {
                        //回调方法
                        loadList();
                    }
                });
            });

            //获取筛选条件
            var getConditions = function () {
                var conditionJSON = [];
                //店铺列表
                if ($('#search-Shop').val().length > 0) {
                    conditionJSON.push({ column: "a.ShopID", value: $('#search-Shop').val(), operator: "in_shopids", connector: "and" });
                }
                //店铺代码,名称
                if ($('#search-store').val().length > 0) {
                    conditionJSON.push({ column: "a.ShopCode", value: $('#search-store').val(), operator: "=", connector: "or" });
                    conditionJSON.push({ column: "shop.ShopName", value: $('#search-store').val(), operator: "=", connector: "and" });
                }
                //货号
                if ($('#search_materialID').val().length > 0) {
                    conditionJSON.push({ column: "a.MaTerialID", value: $('#search_materialID').val(), operator: "=", connector: "and" });
                }
                

                conditionJSON = JSON.stringify(conditionJSON);
                var conditions = encodeURIComponent(conditionJSON);
                return conditions;
            };

        });




    </script>
</asp:Content>
